using System.Collections.Generic;
using Game.Main.Model;
using UnityEngine;

namespace Game.Component
{
	public class WaterDispenserItem : BaseItem
	{
		protected Transform waterOutlet;

		protected Transform water;

		protected SpriteRenderer waterSprite;

		private List<BaseItem> targetItems = new List<BaseItem>();

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			waterOutlet = model.Find("WaterOutlet");
			InitComponentTrigger(waterOutlet.gameObject, ItemComponentTrigger.Type.WaterOutlet, null, null);
			water = model.Find("Water");
			waterSprite = water.GetComponent<SpriteRenderer>();
			waterSprite.color = base.itemData.bodyColor;
		}

		protected void InitComponentTrigger(GameObject obj, ItemComponentTrigger.Type type, ItemComponentTrigger.EnterCollision onEnter, ItemComponentTrigger.ExitCollision onExit)
		{
			obj.layer = 12;
			ItemComponentTrigger itemComponentTrigger = obj.AddComponent<ItemComponentTrigger>();
			itemComponentTrigger.Init(this, type, null, null, null);
		}

		public void AddTargetItem(BaseItem item)
		{
			if (!targetItems.Contains(item))
			{
				targetItems.Add(item);
			}
			UpdateWaterState();
		}

		public void RemoveTargetItem(BaseItem item)
		{
			if (targetItems.Contains(item))
			{
				targetItems.Remove(item);
			}
			UpdateWaterState();
		}

		private void UpdateWaterState()
		{
			if (targetItems.Count < 1)
			{
				water.gameObject.SetActive(false);
			}
			else
			{
				water.gameObject.SetActive(true);
			}
		}
	}
}
